package com.freedy.dataStructure.sort;

import java.util.Arrays;

/**
 * 选择排序
 * @author Freedy
 * @date 2021/3/18 18:58
 */
public class SelectSort {

    public static void main(String[] args) {
        //20个随机数
        int[] arr={368,93,284,507,204,620,331,573,910,977,
                711,305,825,438,698,49,868,241,598,737};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            int min=Integer.MAX_VALUE;//int类型的最大值
            int minIndex = 0;
            for (int j = i; j < arr.length; j++) {
                if (arr[j]<min){
                    min=arr[j];
                    minIndex=j;
                }
            }
            int temp=arr[minIndex];
            arr[minIndex]=arr[i];
            arr[i]=temp;
        }
    }

}
